package com.lostandfound.action;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.lostandfound.entity.Topic;
import com.lostandfound.entity.User;
import com.lostandfound.service.TopicService;
import com.lostandfound.util.ReturnCode;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**  
* 
*  
* @date 2018年3月11日
*/
@ParentPackage("basePackage")
//@Action(value="TopicController")
@Namespace("/")
public class TpoicAction {
	
	@Autowired
	private TopicService topicService;//注入业务层实例
	
	private Topic topic;
	private HashMap<String, Object> pagemap;//存放分页以及查询信息
	private Map<String, Object> res = new HashMap<String, Object>();//用来存储和传递返回结果

	public Map<String, Object> getRes() {
		return res;
	}
	public void setRes(Map<String, Object> res) {
		this.res = res;
	}
	
	public Topic getTopic() {
		return topic;
	}
	public void setTopic(Topic topic) {
		this.topic = topic;
	}
	
	public HashMap<String, Object> getPagemap() {
		return pagemap;
	}
	public void setPagemap(HashMap<String, Object> pagemap) {
		this.pagemap = pagemap;
	}
	
	/**
	 * 获取session中保存的当前登录用户信息
	 * @return
	 */
	private User getUser(){
		Map<String, Object> session = ActionContext.getContext().getSession();
		User loginUser = (User)session.get("loginUser");
		return loginUser;
	}
	
	/**
	 * 根据条件获取主贴列表
	 * 传入userCode可获取该用户的发帖
	 * 传入pname可以获取该板块下的主贴
	 * 传入时间段可以获取该时间段内的主贴
	 * 可以多条件查询
	 * @return
	 */
	@Action(value="listTopic",results={
			@Result(type="json",params={"root","res"})
	})
	public String listTopic(){
		if(getUser() != null){//判断是否是登录状态
			if(pagemap == null){//pagemap存放分页属性
				pagemap = new HashMap<String,Object>();
				pagemap.put("page", "1");
				pagemap.put("size", "10");
			}
			try {
				@SuppressWarnings("unchecked")
				List<Topic> listtopic = (List<Topic>)topicService.listTopicByPage(pagemap);//分页查询主贴信息
				int count = topicService.Count(pagemap);
				this.res.put("topicList", listtopic);
				this.res.put("topiccount",count);//获取总条目
				int size = Integer.parseInt((String)pagemap.get("size"));
				this.res.put("topicpage",count%size>0?count/size+1:count/size);//获取总页数
				this.res.put("code", ReturnCode.SUCCESS);//查询成功，跳转主贴列表显示页
			} catch (Exception e) {
				System.out.println("查询失败！");
				this.res.put("code", ReturnCode.FAIL);//查询失败，返回主页
			}
			return ActionSupport.SUCCESS;
		}
		this.res.put("code", ReturnCode.LOST_NEWS);//用户未登录，跳转登录页
		System.out.println("--请登录--");//没有登录则跳转登录页面
		return ActionSupport.SUCCESS;
	}
	
	/**
	 * 添加主贴
	 */
	@Action(value="addTopic",results={
			@Result(type="json",params={"root","res"})
	})
	public String addTopic(){
		if(this.topic != null){
			User user = getUser();
			this.topic.setT_create_time(new Date());
			this.topic.setUser(user);
			this.topic.setTstatus("0");
			//this.topic.setLost_time( new SimpleDateF pagemap.get("lost_time"));
			try {
				topicService.save(this.topic);
				//System.out.println("保存成功!");
				this.res.put("code", ReturnCode.SUCCESS);//添加成功，跳转添加页可继续添加
			} catch (Exception e) {
				System.out.println("添加板块信息失败！");
				this.res.put("code", ReturnCode.FAIL);//添加失败，跳转添加页可继续编辑
				//this.res.put("addTopic", this.topic);
			}
			return ActionSupport.SUCCESS;
		}
		this.res.put("code", ReturnCode.LOST_NEWS);//添加失败，跳转添加页重新添加
		return ActionSupport.SUCCESS;
	}
	
	/**
	 * 根据主贴id获取主贴信息
	 * @return
	 */
	@Action(value="getTopicById",results={
			@Result(type="json",params={"root","res"})
	})
	public String getTopicById(){
		if(this.topic != null){
			Long tid = this.topic.getTid();
			try {
				this.topic = topicService.selectByPrimary(tid);
				this.res.put("topic", this.topic);
				this.res.put("code", ReturnCode.SUCCESS);
			} catch (Exception e) {
				System.out.println("获取主贴信息失败！");
				this.res.put("code", ReturnCode.FAIL);
			}
			return ActionSupport.SUCCESS;
		}
		this.res.put("code", ReturnCode.LOST_NEWS);//输入有误
		return ActionSupport.SUCCESS;
	}
	
	/**
	 * 案件完成
	 * @return
	 */
	@Action(value="endTopic",results={
			@Result(type="json",params={"root","res"})
	})
	public String endTopic(){
		if(this.topic != null){
			Long tid = this.topic.getTid();
			try {
				this.topic = topicService.selectByPrimary(tid);
				this.topic.setTstatus("1");
				topicService.save(this.topic);
				this.res.put("topic", this.topic);
				this.res.put("code", ReturnCode.SUCCESS);
			} catch (Exception e) {
				System.out.println("操作失败！");
				this.res.put("code", ReturnCode.FAIL);
			}
			return ActionSupport.SUCCESS;
		}
		this.res.put("code", ReturnCode.LOST_NEWS);
		return ActionSupport.SUCCESS;
	}
	
	/**
	 * 修改主贴信息
	 * @return
	 */
	@Action(value="updateTopic",results={
			@Result(type="json",params={"root","res"})
	})
	public String updateTopic(){
		if(this.topic != null){
			try {
				Topic newtopic = topicService.selectByPrimary(this.topic.getTid());
				//newtopic.setT_end_time(new Date());
				newtopic.setTcontent(this.topic.getTcontent());
				newtopic.setTname(this.topic.getTname());
				topicService.update(newtopic);
				this.res.put("code", ReturnCode.SUCCESS);
			} catch (Exception e) {
				System.out.println("更新板块信息失败！");
				this.res.put("code", ReturnCode.FAIL);
				//this.res.put("updateTopic", this.topic);
			}
			return ActionSupport.SUCCESS;
		}
		this.res.put("code", ReturnCode.LOST_NEWS);//输入有误
		return ActionSupport.SUCCESS;
	}
	
	/**
	 * 批量删除主贴
	 * @return
	 */
	@Action(value="delTopic", results={
			@Result(type="json", params={"root","res"})
			})
	public String delTopic(){
		if(this.res != null && this.res.get("ids") != null){
			String[] ids = (String[])this.res.get("ids");
		
			String tids = ids[0]; 
			tids = tids.substring(1, tids.length()-1);
			String[] id = tids.split(",");
			if(id != null && id.length >0){
				Long[] t_ids = new Long[id.length];
				for (int i=0;i<id.length;i++) {
					Long l1 = Long.parseLong(id[i]);
					t_ids[i] = l1;
				}
				try {
					topicService.deleteTopicByBranch(t_ids);//删除主贴
					this.res.put("code", ReturnCode.SUCCESS);//删除成功，页面清除指定删除项
				} catch (Exception e) {
					System.out.println("删除主贴信息失败！");
					this.res.put("code", ReturnCode.FAIL);//删除失败，请重试
				}
				return ActionSupport.SUCCESS;
			}
		}
		this.res.put("code", ReturnCode.LOST_NEWS);//输入有误,请选择要删除的主贴
		return ActionSupport.SUCCESS;
	}
	
	
	
	
	

}
